<?php

namespace App\Console\Commands;

use App\Models\Cate;
use Illuminate\Console\Command;

class CateCommand extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'system:cate';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Command description';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		$cateJson = 'resources/json/cate.json';
		$json = file_get_contents($cateJson);
		$cateData = json_decode($json, true);
		$section = $cateData['data']['section'];
		foreach ($section as $value) {
			Cate::insert(['cate_id' => $value['order'], 'name' => $value['name']]);
			foreach ($value['list'] as $cate) {
				Cate::insert(['pid' => $value['order'], 'name' => $cate['name'], 'cate_id' => $cate['term'], 'short_catid' => $cate['shortcatid']]);
			}
		}
	}
}
